package traveldream.ejb.entities;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.persistence.*;

import traveldream.ejb.DTO.PackageDTO;

/**
 * The persistent class for the default_package database table.
 * 
 */
@Entity
@Table(name="default_package")
@NamedQueries({@NamedQuery(name="DefaultPackage.findAll", query="SELECT d FROM DefaultPackage d WHERE d.Invalid=false"),	
@NamedQuery(name="DefaultPackage.search",query="SELECT d FROM DefaultPackage d JOIN d.transport_dep tD WHERE tD.arrival_Place = :arr_place AND tD.departure_Date = :dep_date AND tD.departure_Place = :dep_place AND d IN (SELECT d FROM DefaultPackage d JOIN d.transport_arr tA WHERE d.Invalid=false AND ta.arrival_Date = :arr_date)"),
@NamedQuery(name="DefaultPackage.findByG", query="SELECT d FROM DefaultPackage d WHERE d.Invalid=false AND d.transport_dep = :g"),
@NamedQuery(name="DefaultPackage.findByR", query="SELECT d FROM DefaultPackage d WHERE d.Invalid=false AND d.transport_arr = :r"),
@NamedQuery(name="DefaultPackage.findByE", query="SELECT d FROM DefaultPackage d WHERE d.Invalid=false AND d.excursion = :e"),
@NamedQuery(name="DefaultPackage.findByH", query="SELECT d FROM DefaultPackage d WHERE d.Invalid=false AND d.hotel = :h")
})
public class DefaultPackage implements Serializable {
	private static final long serialVersionUID = 1L;

	public static final String FIND_ALL = "DefaultPackage.findAll";
	
	@Id
	@GeneratedValue(strategy=GenerationType.IDENTITY)
	private int id;

	private String description;

	@ManyToOne
	@JoinColumn(name="`Id Arrival Transport`")
	private TransportationMean transport_arr;

	@ManyToOne
	@JoinColumn(name="`Id Departure Transport`")
	private TransportationMean transport_dep;

	@ManyToOne
	@JoinColumn(name="`Id Hotel`")
	private Hotel hotel;

	private String name;

	private String nation;

	private int price;

	//bi-directional many-to-one association to Excursion
	@ManyToOne
	@JoinColumn(name="`Id Excursion`")
	private Excursion excursion;
	
	private boolean Invalid;
	
	
	@JoinTable(
		name="purchased_by"
		, joinColumns={
			@JoinColumn(name="Id_Package", referencedColumnName="Id")
			}
		, inverseJoinColumns={
			@JoinColumn(name="Email_Customer", referencedColumnName="idUser")
			}
		)
	@ManyToMany
	private List<User> buyers;
	
	public List<User> getBuyers() {
		return buyers;
	}

	public void setBuyers(List<User> buyers) {
		this.buyers = buyers;
	}
	
	public PackageDTO convertToDTO(){
		PackageDTO pDTO = new PackageDTO();
	    pDTO.setDescription(description);
    	pDTO.setId(id);
    	pDTO.setName(name);
    	pDTO.setNation(nation);
    	pDTO.setPrice(price);
    	pDTO.setIdReturn(transport_arr.getId());
    	pDTO.setIdGoing(transport_dep.getId());
    	if(excursion!=null)
    		pDTO.setIdExcursion(excursion.getId());
    	pDTO.setIdHotel(hotel.getId());
    	pDTO.setInvalid(Invalid);
    	return pDTO;
    }
	    

	public boolean isInvalid() {
		return Invalid;
	}

	public void setInvalid(boolean invalid) {
		Invalid = invalid;
	}

	public DefaultPackage() {
	}

	public int getId() {
		return this.id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getDescription() {
		return this.description;
	}

	public TransportationMean getTransport_arr() {
		return transport_arr;
	}

	public void setTransport_arr(TransportationMean transport_arr) {
		this.transport_arr = transport_arr;
	}

	public TransportationMean getTransport_dep() {
		return transport_dep;
	}

	public void setTransport_dep(TransportationMean transport_dep) {
		this.transport_dep = transport_dep;
	}

	public Hotel getHotel() {
		return hotel;
	}

	public void setHotel(Hotel hotel) {
		this.hotel = hotel;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public String getName() {
		return this.name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getNation() {
		return this.nation;
	}

	public void setNation(String nation) {
		this.nation = nation;
	}

	public int getPrice() {
		return this.price;
	}

	public void setPrice(int price) {
		this.price = price;
	}

	public Excursion getExcursion() {
		return this.excursion;
	}

	public void setExcursion(Excursion excursion) {
		this.excursion = excursion;
	}

}